import React, { Component } from 'react';
import { AsyncStorage } from 'react-native';

export default class SessionUtilsUtils extends React.Component
{
    static get(key)
    {
        return AsyncStorage.getItem(key).then((value) =>
        {
            const jsonValue = JSON.parse(value);
            return jsonValue;
        });
    }

    static save(key, value)
    {
        return AsyncStorage.setItem(key, JSON.stringify(value));
    }

    static update(key, value)
    {
        return AsyncStorage.get(key).then((item) =>
        {
            value = typeof value === 'string' ? value : Object.assign({}, item, value);
            return AsyncStorage.setItem(key, JSON.stringify(value));
        });
    }

    static delete(key)
    {
        return AsyncStorage.removeItem(key);
    }
}
